package com.markby.fscm.controller;

import com.markby.fscm.common.Result;
import com.markby.fscm.domain.form.KeyFrom;
import com.markby.fscm.domain.form.LoginUser;
import com.markby.fscm.service.AdminSettingsService;
import com.markby.fscm.service.KeyStoreService;
import com.markby.fscm.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: soft-server-cipher-machine
 * @description: 登录
 * @author: baoyu
 * @create: 2024-08-10 18:31
 **/
@Tag(name = "登录")
@RequestMapping("/admin")
@RestController
public class LoginController {

    @Autowired
    AdminSettingsService adminSettingsService;

    @Autowired
    KeyStoreService keyStoreService;

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginUser user) {

        adminSettingsService.verifyPassword(user.getPassword(), user.getSource());

        String token = JwtUtil.createToken(user.getRandomNum(), user.getSource());

        return Result.ok(token);
    }

//    @Operation(summary = "获取登陆用户source信息")
//    @GetMapping("sourceInfo")
//    public Result<String> sourceInfo() {
//        return Result.ok(LoginUserHolder.getLoginUser().getSource());
//    }


    @Operation(summary = "获取设备信息")
    @GetMapping("getDeviceInfo")
    public Result<String> getDeviceInfo() {
        // TODO 获取设备信息
        return Result.ok("TODO");
    }

    // TODO 初始化

    // TODO 生成密钥
    @Operation(summary = "生成密钥")
    @PostMapping("generateKey")
    public Result<String> generateKey(@RequestBody KeyFrom keyFrom) {

        keyStoreService.generateKey(keyFrom);

        return Result.ok("0");
    }

    // TODO 黑白名单

}
